﻿Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Text.RegularExpressions

Public Class MSIBuilder

    Private m_SourceZIPFilePath As String
    Public Property SourceZIPFilePath() As String
        Get
            Return m_SourceZIPFilePath
        End Get
        Set(ByVal value As String)
            m_SourceZIPFilePath = value
        End Set
    End Property
    Private m_TargetZIPFilePath As String
    Public Property TargetZIPFilePath() As String
        Get
            Return m_TargetZIPFilePath
        End Get
        Set(ByVal value As String)
            m_TargetZIPFilePath = value
        End Set
    End Property
    Private m_ZIPFileName As String
    Public Property ZIPFileName() As String
        Get
            Return m_ZIPFileName
        End Get
        Set(ByVal value As String)
            m_ZIPFileName = value
        End Set

    End Property
    Private m_MSITemplatePath As String
    Public Property MSITemplatePath() As String
        Get
            Return m_MSITemplatePath
        End Get
        Set(ByVal value As String)
            m_MSITemplatePath = value
        End Set

    End Property
    Private m_SEDTemplatePath As String
    Public Property SEDTemplatePath() As String
        Get
            Return m_SEDTemplatePath
        End Get
        Set(ByVal value As String)
            m_SEDTemplatePath = value
        End Set

    End Property
    Private m_ProductCode As String
    Public Property ProductCode() As String
        Get
            Return m_ProductCode
        End Get
        Set(ByVal value As String)
            m_ProductCode = value
        End Set

    End Property
    Private m_vdproj As String
    Public Property Vdproj() As String
        Get
            Return m_vdproj
        End Get
        Set(ByVal value As String)
            m_vdproj = value
        End Set

    End Property


    Public Function MakeMSI() As Boolean
        Try

            Dim args As New StringBuilder()
            Dim p As New Process()
            args.Append(MSITemplatePath)
            args.Append(" /build Debug")
            Dim pStartInfo As New ProcessStartInfo("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv", args.ToString())
            DisplayLog(args.ToString())
            pStartInfo.UseShellExecute = False
            pStartInfo.RedirectStandardOutput = True
            pStartInfo.RedirectStandardError = True
            pStartInfo.RedirectStandardInput = True
            pStartInfo.CreateNoWindow = True
            p.StartInfo = pStartInfo
            DisplayLog("------ Build started: Project: CM_BuildTemplate AT " + DateTime.Now.ToString() + "------")
            p.Start()
            p.WaitForExit()
            DisplayLog("========== Build:  succeeded or up-to-date 0 failed, 0 skipped  AT " + DateTime.Now.ToString() + "------")
            MoveZipFile()
            DisplayLog("========== Move:  " + ZIPFileName + ",succeeded ==========")
            p = New Process()
            args = New StringBuilder()
            args.Append(" /N /Q")
            args.Append(" " + SEDTemplatePath + " ")
            pStartInfo = New ProcessStartInfo("C:\Windows\System32\IExpress.exe", args.ToString())
            pStartInfo.UseShellExecute = False
            pStartInfo.RedirectStandardOutput = True
            pStartInfo.RedirectStandardError = True
            pStartInfo.RedirectStandardInput = True
            pStartInfo.CreateNoWindow = True
            p.StartInfo = pStartInfo
            DisplayLog("========== IExpress: started  AT " + DateTime.Now.ToString() + "==========")
            p.Start()
            p.WaitForExit()
            DisplayLog("========== IExpress: succeeded AT " + DateTime.Now.ToString() + "==========")

        Catch ex As Exception
            DisplayLog(ex.ToString())
        End Try

    End Function

    Function MoveZipFile() As Boolean
        Try

            FileCopy(SourceZIPFilePath, TargetZIPFilePath + "\" + ZIPFileName)
            DisplayLog("### File Moved to " + TargetZIPFilePath + "\" + ZIPFileName + " At " + DateTime.Now.ToString() + " ###")
        Catch ex As Exception
            DisplayLog("### Error in Moving File  : " + ex.ToString() + " ###")
        End Try
    End Function

    Public Sub DisplayLog(ByVal logDetails As String)
        Console.WriteLine(logDetails)
    End Sub

End Class
